package com.socialuni.sdk.service;

import com.socialuni.sdk.api.model.RO.user.base.SocialUserRO;
import com.socialuni.sdk.domain.UserProviderLoginDomain;
import com.socialuni.sdk.manage.CommonUserTokenDOCreate;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.model.DO.user.TokenDO;
import com.socialuni.sdk.model.ProviderLoginQO;
import com.socialuni.sdk.model.RO.UniLoginRO;
import com.socialuni.sdk.utils.CommonUserROUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

@Service
public class CommonUserLoginService {
    @Resource
    CommonUserTokenDOCreate commonUserTokenDOCreate;
    @Resource
    UserProviderLoginDomain userProviderLoginDomain;

    //通过渠道信息返回用户
    @Transactional
    public UniLoginRO<SocialUserRO> providerLogin(ProviderLoginQO loginQO) {
        //创建或返回
        UserDO user = userProviderLoginDomain.getOrCreate(loginQO);

        return this.createTokenAndToUserRO(user);
    }

    @Transactional
    public UniLoginRO<SocialUserRO> createTokenAndToUserRO(UserDO user) {
        //创建或返回
        SocialUserRO userRO = CommonUserROUtil.getMineUser(user);

        TokenDO socialUserTokenDO = commonUserTokenDOCreate.create(user.getId());

        return new UniLoginRO<>(socialUserTokenDO.getToken(), userRO);
    }
}